package com.iris.live.services.data.repositories;

import com.iris.live.services.data.models.SibmbsSettingModel;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.Date;
import java.util.List;

/**
 *
 */
public interface SibmbsSettingRepository extends JpaRepository<SibmbsSettingModel, Integer>, JpaSpecificationExecutor<SibmbsSettingModel> {

    @Query("select s from SibmbsSettingModel s where s.vin=?1  and s.permit=?2 and s.siBmbsCode=?3 ")
    SibmbsSettingModel findSisettingByVinAndCode(String vin, String permit, String code);

    @Query("select s from SibmbsSettingModel s where s.carTypeId=?1  and s.permit=?2 and s.siBmbsCode=?3 ")
    SibmbsSettingModel findSisettingByCarCodeAndSiCode(Integer carTypeId, String permit, String code);

    @Query("select s from SibmbsSettingModel s where s.pNum=?1  and s.permit=?2 and s.siBmbsCode=?3 and s.validStartDate=?4 and s.validEndDate =?5  and  s.carTypeId= ?6 and s.setType=?7")
    SibmbsSettingModel findSisettingBypNumAndSiCode(Integer pNum, String permit, String code,Date start,Date end,Integer carTypeId,Integer type);

    @Query("select max(s.pNum) from SibmbsSettingModel as s")
    Integer findMaxPnum();

    @Query("select s  from SibmbsSettingModel  s where s.pNum = ?1 and s.permit=?2 ")
    List<SibmbsSettingModel> findListByPnum(Integer pNum, String permit);

    @Modifying
    @Query("delete  from SibmbsSettingModel s where s.pNum = ?1 and s.permit =?2")
    void delete(Integer pNum, String permit);

    @Modifying
    @Query("delete  from SibmbsSettingModel s where s.pNum = ?1 and s.permit =?2 and s.stockId =?3")
    void deleteStock(Integer pNum, String permit,Integer stockId);

}
